<?php
namespace Index\Controller;
use Think\Controller;
class IndexController extends CommonController {
    public function indexAction($where='',$count=null){

    	$sql = "select count(id) count from question";

    	$count = is_null($count) ? current(M()->query($sql))['count'] : $count;

    	// var_dump($count);

    	$Page  = new \Think\Page($count,3);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show  = $Page->show();// 分页显示输出

    	$sql = "select t1.*,t2.username from question t1 left join user t2 on t1.uid=t2.id ".$where." limit {$Page->firstRow},{$Page->listRows}";

    	// echo  $show;
    	// echo $sql;
    	// exit;

    	$questions=M()->query($sql);

    	foreach ($questions as &$v) {

    		if($v['answer_num']>0){
    			$sql ="SELECT t2.username,t1.create_time from answer t1 LEFT JOIN user t2 ON t1.uid=t2.id where qid={$v['id']} order by create_time desc limit 1 ";
    			$row = current(M()->query($sql));

    			$v['username']=$row['username'];
    			$v['create_time']=$row['create_time'];
    		}

    		$sql = "select t1.cid,t2.cname from category_question t1 left join category t2 on t1.cid=t2.id where t1.qid={$v['id']}";

    		$v['category'] = M()->query($sql);

    		$v['create_time'] = Ftime(date('Y-m-d H:i:s',$v['create_time']));
    	}

    	$this->assign('show',$show);

    	$this->assign('questions',$questions); 
    	
       	$this->display('index/index');
    }
    public function hotAction()
    {
    	$this->indexAction('order by view desc');
    }

    public function unanswerAction()
    {
    	$this->indexAction("where answer_num='0'");
    }
}